package com.pine.dbant.common.enums;

import lombok.Getter;

/**
 * 数据源类型
 */
@Getter
public enum DatasourceType {
    CACHE(0, "CACHE","com.intersys.jdbc.CacheDriver", "select 1"),
    SQLSERVER(1, "SQLSERVER","com.microsoft.sqlserver.jdbc.SQLServerDriver","select 1"),
    ORACLE(2,"ORACLE","oracle.jdbc.OracleDriver","select 1 from dual"),
    MYSQL(3,"MYSQL","com.mysql.jdbc.Driver","select 1"),
    HIVE(4,"HIVE","org.apache.hive.jdbc.HiveDriver","select 1"),
    DORIS(5,"DORIS","com.mysql.jdbc.Driver","select 1");
    /**
     * 数据类型值
     */
    private final int value;
    /**
     * 数据库名称
     */
    private final String label;
    /**
     * jdbc驱动类名称
     */
    private final String driverClassName;
    /**
     * 查询校验sql
     */
    private final String validationQuery;

    /**
     * Return the integer value of this status code.
     */
    public int value() {
        return this.value;
    }
    DatasourceType(int value, String label, String driverClassName, String validationQuery) {
        this.value = value;
        this.label = label;
        this.driverClassName=driverClassName;
        this.validationQuery=validationQuery;
    }

    public static DatasourceType getByValue(int value) {
        DatasourceType[] values = DatasourceType.values();
        for (DatasourceType e : values) {
            if (e.value == value) {
                return e;
            }
        }
        return null;
    }


    public static DatasourceType fromUrl(String url) {
        if (url == null) {
            return null;
        }
        url = url.toLowerCase();
        if (url.contains("cache")) {
            return DatasourceType.CACHE;
        } else if (url.contains("mysql")) {
            return DatasourceType.MYSQL;
        } else if (url.contains("oracle")) {
            return DatasourceType.ORACLE;
        } else if (url.contains("sqlserver")) {
            return DatasourceType.SQLSERVER;
        }
        return null;
    }

}
